package com.idol.service.impl;

import com.idol.dao.AuthCodeMapper;
import com.idol.entity.AuthCode;
import com.idol.service.ICodeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

/**
 * @author SupremeSir
 * @description 验证码服务接口
 * @className CodeServiceImpl
 * @date 2021/2/15 21:32
 **/
@Service
public class CodeServiceImpl implements ICodeService {
    @Autowired
    private AuthCodeMapper authCodeMapper;

    @Override
    public String saveCode(String email) {
        // 随机生成六位数验证码
        long codeNum = Math.round(Math.random() * 1000000);
        String codeStr = String.valueOf(codeNum);
        AuthCode authCode = new AuthCode();
        authCode.setEmail(email);
        authCode.setCode(codeStr);
        long currentTime = System.currentTimeMillis();
        Date createtime = new Date(currentTime);
        authCode.setCreatetime(createtime);
        // 设置验证码失效时间
        Date expiretime = new Date(currentTime + 600000);
        authCode.setExpiretime(expiretime);
        authCodeMapper.insert(authCode);
        return codeStr;
    }
}
